﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TestVDPlayer : MonoBehaviour {

    private VDPlayer player;
    private bool wait = false;
    private _Frame frame;
    public RawImage rimg;
    private Texture2D tex;
    public string url = "F:/HTTPServer/synctest.mp4";
    private void Awake()
    {
        VDModule.SetVDModuleLog();
    }
    // Use this for initialization
    void Start () {
        player = new VDPlayer();
        player.OnFrameCb += OnFrame;
        player.SetOption(VDOptionType.OT_OutputPixFmt, VDModule.OUTPUT_PIXFMT_RGBA);
        player.SetOption(VDOptionType.OT_Loop, 1);
        //player.SetOption(VDOptionType.OT_AutoPlay, 0);
        //player.SetOption(VDOptionType.OT_OpenModel, VDModule.OPEN_MODEL_CAMERA);
        player.Open(url, opencb);
        //player.Open("F:/HTTPServer/4K.mp4", opencb);
    }

    private void OnFrame(_Frame frame)
    {
        if (wait) return;
        this.frame = frame;
        wait = true;
    }

    private void opencb(_DemuxInfo info)
    {
        player.StartDemux(true, true);
    }

    // Update is called once per frame
    void Update () {
        if (!wait) return;
        if (tex == null)
            tex = new Texture2D(frame.width, frame.height, TextureFormat.RGBA32, false);

        tex.LoadRawTextureData(frame.data[0], frame.width * frame.height * 4);
        tex.Apply();
        rimg.material.SetTexture("_RawImg", tex);
        wait = false;
	}

    public void Play()
    {
        if (player.IsPlaying)
            player.Pause();
        else
            player.Play();
    }
    private void OnDestroy()
    {
        player.Release();
    }
}
